<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    #pur {
        font-size: 16px !important;
        width: 1600px;
    }

    #pur tr th {
        background: #FFFFFF !important;
    }

    #tableBody tr td {
        background: #FFFFFF !important;
    }

    #pur tr th {
        border: #000000 1px solid !important;
    }

    #pur tr td {
        border: #000000 1px solid !important;
    }

    #tableTitle {
        font-size: 35px !important;
        text-align: center !important;
        vertical-align: middle !important;
    }

    #purTableFoot tr td {
        border: 0 !important;
        padding: 5px;
    }

    #head1 {
        font-size: 20px;
    }

    #head1 th {
        text-align: center;
        vertical-align: middle;
        height: 50px;
    }
</style>
<body class="hold-transition skin-blue sidebar-mini">

<!--变更接收-->
<div class="modal fade" id="modal-change">
    <div class="modal-dialog" style="width:1350px;padding-top: 5px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">接收变更处理</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div id="content">
                        <table id="pur" style="width:1320px;float: left">
                            <thead id="purTableHead">
                            <tr>
                                <th id="tableTitle" colspan="13" style="height:90px">
                                    外购件计划变更单
                                </th>
                            </tr>
                            <tr id="head1">
                                <th colspan="2" style="width: 100px;height:75px">项目编号</th>
                                <th id="projectCode" style="width:150px"></th>
                                <th style="width: 100px">项目名称</th>
                                <th id="projectName" colspan="3" style="width: 200px"></th>
                                <th style="width: 100px">项目数量</th>
                                <th id="projectNum" style="width: 90px"></th>
                                <th style="width: 100px">类型</th>
                                <th id="projectType" style="width: 80px"></th>
                                <th style="width: 100px">部分</th>
                                <th id="projectPart" style="width: 80px"></th>
                            </tr>
                            </thead>
                            <tbody id="tableBody">
                            <tr style="text-align: center;vertical-align: middle;font-size: 16px">
                                <td></td>
                                <td>序号</td>
                                <td colspan="2">物料描述</td>
                                <td>数量</td>
                                <td>单位</td>
                                <td colspan="3">型号/标准号</td>
                                <td>品牌</td>
                                <td>备注</td>
                                <td>原因</td>
                                <td>下载</td>
                            </tr>
                            </tbody>
                            <tfoot id="purTableFoot">
                            <tr style="vertical-align: middle;font-size: 16px">
                                <td colspan="2">提报人：</td>
                                <td id="designer"></td>
                                <td>联系电话：</td>
                                <td id="designerTel" colspan="2"></td>
                                <td colspan="2">审核：</td>
                                <td id="reviewer" colspan="2"></td>
                                <td>批准：</td>
                                <td id="approver" colspan="2"></td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <table id="bootstrap-table-change-view"></table>
            </div>
            <div class="modal-footer">
                <button shiro:hasPermission="buy:planchangereceive:edit" type="button" class="btn btn-success"
                        id="btn-confirm">确认变更并接收
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<!--接收变更处理-->
<div class="modal fade" id="modal-edit">
    <div class="modal-dialog" style="width:1200px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">接收变更处理</h4>
            </div>
            <div class="modal-body" style="height: 700px">
                <div class="col-md-12">
                    <div class="col-md-5 col-md-offset-1">
                        <form id="after" class="form-horizontal">
                            <input type="hidden" name="id">
                            <div class="row">
                                <div class="form-group">
                                    <label class="col-sm-12 text-center">变更后</label>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">物料描述</label>
                                    <div class="col-sm-8">
                                        <span id="afterMalDesc"
                                              class="bg-gray-light form-control-static"
                                              style="word-break:break-all;
                                                    word-wrap:break-all;
                                                    border: 1px solid #ccc;
                                                    border-radius: 4px;
                                                    display: inline-block;
                                                    -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
                                                    box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);"
                                        ></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">数量</label>
                                    <div class="col-sm-8">
                                        <span id="afterNum" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">单位</label>
                                    <div class="col-sm-8">
                                        <span id="afterUnit" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div id="afterStandardDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">型号/标准号</label>
                                    <div class="col-sm-8">
                                        <span id="afterStandard" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div id="afterDiagramDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">图号</label>
                                    <div class="col-sm-8">
                                        <span id="afterDiagram" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">品牌</label>
                                    <div class="col-sm-8">
                                        <span id="afterBrand" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div id="afterAssistTypeDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">外协类型</label>
                                    <div class="col-sm-8">
                                        <span id="afterAssistType" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">备注</label>
                                    <div class="col-sm-8">
                                        <span id="afterRemark"
                                              class="form-control bg-gray-light"
                                              style="word-break:break-all;display:block;white-space:pre-wrap;height:200px;font-weight:bold;color:#0066CC;"></span>
                                    </div>
                                </div>
                                <div id="afterUseStoreDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">是否使用库存</label>
                                    <div class="col-sm-8">
                                        <span id="afterUseStore" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">采购类型</label>
                                    <div class="col-sm-8">
                                        <span id="rppTypes" class="form-control bg-gray-light"></span>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-5 col-md-offset-1">
                        <form id="change" class="form-horizontal">
                            <input type="hidden" style="display: none" name="id">
                            <div class="row">
                                <div class="form-group">
                                    <label class="col-sm-12 text-center">编辑区</label>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">物料描述</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" name="malDesc" type="text" maxlength="255"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">数量</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" name="num" type="type"
                                               oninput="numInput(this)"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">单位</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" name="unit" type="text" maxlength="255"/>
                                    </div>
                                </div>
                                <div id="standardDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">型号/标准号</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" name="standard" type="text" maxlength="255"/>
                                    </div>
                                </div>
                                <div id="diagramDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">图号</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" name="diagram" type="text" maxlength="255"/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">品牌</label>
                                    <div class="col-sm-8">
                                        <input class="form-control" name="brand" type="text" maxlength="255"/>
                                    </div>
                                </div>
                                <div id="assistTypeDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">外协类型</label>
                                    <div class="col-sm-8">
                                        <select name="assistType" class="form-control">
                                            <option value=""></option>
                                            <option value="整体外协">整体外协</option>
                                            <option value="带料外协">带料外协</option>
                                            <option value="锻件毛坯">锻件毛坯</option>
                                            <option value="铸件毛坯">铸件毛坯</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 text-right control-label">备注</label>
                                    <div class="col-sm-8">
                                        <textarea rows="8" cols="25" style="resize:none" class="form-control"
                                                  name="remark" type="text" maxlength="255"></textarea>
                                    </div>
                                </div>
                                <div id="useStoreDiv" class="form-group">
                                    <label class="col-sm-4 text-right control-label">是否使用库存</label>
                                    <div class="col-sm-8">
                                        <select class="form-control" name="useStore">
                                            <option value="1">是</option>
                                            <option value="0">否</option>
                                        </select>
                                        <input class="form-control" name="url" type="hide" style="display:none;" maxlength="255"/>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" id="btn-save">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>

    </div>
</div>
<section class="content-header"><h1>提报计划变更接收</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" name="rppMatDesc" class="form-control" placeholder="请输入物料描述">
                                </div>
                                <div class="form-group">
                                    <label>提报人</label>
                                    <input type="text" name="rppDesigner" class="form-control" placeholder="请输入提报人">
                                </div>
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="proCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="proName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label>相关计划员</label>
                                    <input type="text" name="rppPlanner" class="form-control" placeholder="请输入计划员">
                                </div>
                                <div class="form-group">
                                    <label>接收状态</label>
                                    <select name="recMark" class="form-control">
                                        <option value="-1">全部</option>
                                        <option value="0" selected="selected">未接收</option>
                                        <option value="1">已接收</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>变更创建时间</label>
                                    <input type="text" class="form-control datepicker" name="changeCreateTimeStart"
                                           readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" name="changeCreateTimeEnd"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>

                        </div>
                    </form>
                    <!--                    <div class="row" id="toolbar">-->
                    <!--                        <div class="form-inline">-->
                    <!--                            <button class="btn btn-success pull-right" style="margin-right: 10px" onclick="receiveAll()">-->
                    <!--                                <span class="Bold">批量接收</span>-->
                    <!--                            </button>-->
                    <!--                        </div>-->
                    <!--                    </div>-->
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let imageFileType = ['BMP', 'JPG', 'PNG', 'TIF', 'GIF', 'PCX', 'TGA', 'EXIF', 'FPX', 'SVG', 'PSD', 'CDR', 'PCD', 'DXF', 'UFO', 'EPS', 'AI', 'RAW', 'WMF', 'WEBP'];
    /** 表格 */
    let table, changeTable;
    /** 状态颜色 */
    let reportColors = ['', "label-warning", "label-info", "label-primary", "label-success"];
    /** 状态 */
    let reportState = ['无状态', '使用库存', '正常提报', '暂不提报','甲供'];
    /** 记录当前处理的数据的数组 */
    let reportPlanArray = new Array();
    let clientIp = [[${clientIp}]];
    $(function () {
        $("title").html($("title").html() + " —— 提报计划变更接收");
        /** 初始化表格 */
        table = js.table.init({
            url: ctx + "plan/change/receive/list",
            showExport: false,
            showColumns: false,
            pageSize: 300,
            uniqueId: 'id',
            paginationVAlign: 'both',
            columns: [
                {
                    checkbox: true,
                    width: '30px',
                    formatter: function (value, row, index) {
                        // 正常提报并且数据正常状态可以提交选择
                        if (row.recMark != null && row.recMark === 0) {
                            return {disabled: false}
                        } else {
                            return {disabled: true}
                        }
                    }
                },
                {
                    title: '序号', field: 'id', width: '50px',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {
                    title: "相关计划员", field: 'rppPlanner',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {title: "项目编号", field: 'proCode'},
                {title: "项目名称", field: 'proName'},
                {title: "项目<BR>数量", field: 'proNum'},
                {title: "物料类型", field: 'malType'},
                {title: "部分", field: 'part'},
                {title: "变更<BR>原因", field: 'changeReason',width: '50'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 40px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {title: "提报人", field: 'postName'},
                {title: "提报人<BR>联系方式", field: 'postTel',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {title: "物料描述", field: 'malDesc',width: '110'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 100px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }},
                {title: "数量", field: 'displayNum'},
                {title: "单位", field: 'unit'},
                {
                    title: "型号/标准号", field: 'standard',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "图号", field: 'diagram',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "品牌", field: 'brand',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "是否使<br>用库存", field: 'useStore',
                    formatter: function (value, row) {
                        if (value === null) {
                            return "--";
                        } else if (value === 0) {
                            return "否";
                        } else if (value === 1){
                            return "是";
                        }
                    }
                },
                {
                    title: "外协类型", field: 'assistType',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },
                {
                    title: "备注", field: 'remark',width: '70'
                    ,formatter: function (value, row, index) {
                        if (value != null) {
                            return '<p style="word-wrap: break-word;width: 60px">' + value + '</p>';
                        }  else {
                            return "--";
                        }
                    }
                },{
                    title: "变更创建时间", field: 'changeCreateTime'
                    , formatter: function (value, row) {
                        if (value != null) {
                            return value;
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "附件", field: 'extendUrl', formatter: function (value, row) {
                        if (value != null) {
                            let elements = [];
                            if (hasP('buy:planchangereceive:edit')) {
                                elements.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="look(' + row.id + ',' + row.recMark + ')"><i class="fa  fa-search"></i> 查看</a> ');
                            }
                            if (hasP('buy:planchangereceive:edit')) {
                                elements.push('<a class="btn btn-primary btn-xs" href="/buy/getReportPlanList/download/' + row.id + '/' + row.recMark + '"><i class="glyphicon glyphicon-download-alt"></i> 下载</a> ');
                            }
                            return elements.join('');
                        } else {
                            return "--";
                        }

                    }
                },
                {
                    title: "操作",
                    width: '110px',
                    formatter: function (value, row) {
                        let actions = [];
                        if (hasP('buy:planchangereceive:edit')) {
                            if ((row.rppPlanner === null && checkEmployIsRecipient(sessionUser.name)) || sessionUser.name === row.rppPlanner) {
                                // 未接收状态
                                if (row.recMark === 0) {
                                    actions.push('<button class="btn btn-success btn-xs" onclick="receive(' + row.id + ','+row.changeId+')"> 接收</button> ');
                                }
                                // 已接收状态
                                if (row.recMark === 1) {
                                    actions.push('<button class="btn btn-success btn-xs" onclick="revoke(' + row.id + ')"> 撤销</button> ');
                                }
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });


        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });
        /** 保存到内存数组当中 */
        $("#btn-save").click(function () {
            if ($("#rppTypes").html() === '外协件' && ($("#change [name='assistType']").val() === '' || $("#change [name='diagram']").val() === '')) {
                js.modal.warning("外协件必须选择外协类型和输入图号！");
                return;
            } else if (Number($("#change [name='num']").val()) > Number($("#afterNum").html())) {
                js.modal.warning("数量不能大于变更后的数量！");
                return;
            } else {
                $("#change").data("bootstrapValidator").resetForm();
                let newReportPlanArray = new Array();
                for (let i = 0; i < reportPlanArray.length; i++) {
                    if (reportPlanArray[i].id != $("#change [name='id']").val()) {
                        newReportPlanArray.push(reportPlanArray[i]);
                    }
                }
                let data = $("#change").serializeObject();
                newReportPlanArray.push(data);
                reportPlanArray = newReportPlanArray;
                let tableData = $("#bootstrap-table-change-view").bootstrapTable('getData');
                for (let i = 0; i < tableData.length; i++) {
                    if (tableData[i].dataType != '变更后' && tableData[i].id == data.id) {
                        tableData[i].malDesc = data.malDesc;
                        tableData[i].num = data.num;
                        tableData[i].unit = data.unit;
                        tableData[i].standard = data.standard;
                        tableData[i].diagram = data.diagram;
                        tableData[i].brand = data.brand;
                        tableData[i].assistType = data.assistType;
                        tableData[i].remark = data.remark;
                        tableData[i].useStore = data.useStore;
                        break;
                    }
                }
                $('#bootstrap-table-change-view').bootstrapTable('updateRow', {
                    index: data.index,
                    replace: false,
                    row: {
                        malDesc: data.malDesc,
                        num: data.num,
                        unit: data.unit,
                        standard: data.standard,
                        diagram: data.diagram,
                        brand: data.brand,
                        assistType: data.assistType,
                        remark: data.remark,
                        useStore: data.useStore
                    }
                })
                js.modal.hide('modal-edit');
            }
        })

        /** 添加表单验证规则 */
        js.validate.init("change", {
            fields: {
                malDesc: {validators: {notEmpty: {message: '请输入物料描述'}}},
                num: {validators: {notEmpty: {message: '请输入数量'}}},
                unit: {validators: {notEmpty: {message: '请输入单位'}}}
            },
        });
        /** 保存到内存数组到数据库中*/
        $("#btn-confirm").click(function () {
            js.post({
                url: ctx + "plan/change/checkChangeMark",
                data: {
                    id: $("#btn-confirm").attr("data-id")
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        let json = JSON.stringify(reportPlanArray);
                        js.post({
                            url: ctx + "plan/change/save/change",
                            data: {
                                paramStr: json,
                                clientIp: clientIp
                            },
                            success: function (result) {
                                js.table.refresh(table);
                                js.modal.hide('modal-change');
                            }
                        })
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });

        })

    });

    /**
     * 查看
     * */
    function look(id, recMark) {
        js.post({
            url: ctx + "buy/getReportPlanList/lookFile",
            data: {
                id: id,
                recMark: recMark
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    let fileExtensions = result.data.substring(result.data.lastIndexOf(".") + 1);
                    if (fileExtensions.toUpperCase() === 'PDF') {
                        window.open(result.data);
                        // window.open([[@{/js/pdf/web/viewer.html}]]+'?file=' + result.data, 'PDF');
                    } else {
                        let isOPen = false;
                        for (let i = 0; i < imageFileType.length; i++) {
                            fileExtensions = fileExtensions.toUpperCase();
                            if (fileExtensions == imageFileType[i]) {
                                isOPen = true;
                                window.open(result.data);
                                break;
                            }
                        }
                        if (isOPen === false) {
                            js.modal.warning("该文件格式是：" + fileExtensions + "文件无法预览!");
                        }
                    }
                } else {
                    js.modal.warning("文件损坏！");
                }
            }
        })


    }

    /**
     * 查看
     * */
    function look2(id, dataType) {
        js.post({
            url: ctx + "buy/getReportPlanList/lookFileByChange",
            data: {
                id: id,
                dataType: dataType
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    let fileExtensions = result.data.substring(result.data.lastIndexOf(".") + 1);
                    if (fileExtensions.toUpperCase() === 'PDF') {
                        window.open(result.data);
                    } else {
                        let isOPen = false;
                        for (let i = 0; i < imageFileType.length; i++) {
                            fileExtensions = fileExtensions.toUpperCase();
                            if (fileExtensions == imageFileType[i]) {
                                isOPen = true;
                                window.open(result.data);
                                break;
                            }
                        }
                        if (isOPen === false) {
                            js.modal.warning("该文件格式是：" + fileExtensions + "文件无法预览!");
                        }
                    }
                } else {
                    js.modal.warning("文件损坏！");
                }
            }
        })
    }


    /**
     * 撤销
     **/
    function revoke(id) {
        js.modal.confirm("您确认要撤销吗？", function () {
            js.post({
                url: ctx + "plan/change/revokeChangeStatus",
                data: {
                    id: id
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                        js.table.refresh(table);
                    }
                }
            })
        })
    }

    /**
     * 接收变更提报计划列表
     * 如果C表无计划则直接接收
     */
    function receive(id,changeId) {
        js.post({
            url: ctx + "plan/change/checkChangeMark",
            data: {
                id: id
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    $("#btn-confirm").attr("data-id", id);
                    js.post({
                        url: ctx + "plan/change/plan/exist",
                        data: {
                            id: id
                        },
                        success: function (result) {
                            if (result.data) {
                                js.post({
                                    url: ctx + "plan/change/plan/list",
                                    data: {
                                        id: id,
                                        changeId:changeId
                                    },
                                    success: function (result) {
                                        $("#projectCode").html(result.data.projectCode);
                                        $("#projectName").html(result.data.projectName);
                                        $("#projectNum").html(result.data.projectNum);
                                        $("#projectType").html(result.data.projectType);
                                        $("#projectPart").html(result.data.projectPart);
                                        $("#designer").html(result.data.designer);
                                        $("#designerTel").html(result.data.designerTel);
                                        $("#reviewer").html(result.data.reviewer);
                                        $("#approver").html(result.data.verifier);
                                        // if(result.data.changeReason!=null){
                                        //     if(result.data.changeReason!=null && result.data.changeReason.trim() != ''){
                                        //         let changeReasonArray = result.data.changeReason.split(',');
                                        //         $("input[type='checkbox'][name='reasonChange']").each(function(){
                                        //             let text = $(this).parent().html().trim();
                                        //             text=text.substr(text.indexOf('>')+1);
                                        //             if(text.indexOf('配合')==0 && text.indexOf('修改')>0){
                                        //                 text='配合修改';
                                        //             }else if(text.indexOf('其他')==0){
                                        //                 text=text.substr(text.indexOf('其他'),2);
                                        //             }
                                        //             if(text.substr(0,text.indexOf('<'))!=''){
                                        //                 text=text.substr(text.indexOf('>')+1);
                                        //             }
                                        //             for (let i = 0; i < changeReasonArray.length; i++) {
                                        //                 let changeReason = changeReasonArray[i];
                                        //                 if(changeReason.indexOf('配合')===0 && changeReason.indexOf('修改')>0){
                                        //                     changeReason='配合修改';
                                        //                 }else if(changeReason.indexOf('其他')===0){
                                        //                     changeReason='其他';
                                        //                 }
                                        //                 if(text==changeReason){
                                        //                     $(this).attr('checked','checked');
                                        //                     if(text==='配合修改'){
                                        //                         $("input[name='reasonChangeCoordination']").val(changeReasonArray[i].substring(changeReasonArray[i].indexOf('(')+1,changeReasonArray[i].indexOf(')')));
                                        //                         $("input[name='reasonChangeCoordination']").attr('disabled','disabled');
                                        //                     }else if(text==='其他'){
                                        //                         $("input[name='other']").val(changeReasonArray[i].substring(changeReasonArray[i].indexOf(':')+1));
                                        //                         $("input[name='other']").attr('disabled','disabled');
                                        //                     }
                                        //                 }
                                        //             }
                                        //             //$(this).attr('re','disabled');
                                        //             // $(this).attr('disabled','disabled');
                                        //             // $(this).css('background-color','#0075FF');
                                        //             // $(this).css('color','#FFFFFF');
                                        //         });
                                        //     }
                                        // }
                                        $("#tableBody").empty();
                                        $("#tableBody").append('<tr  style="text-align: center;vertical-align: middle;font-size: 16px">' +
                                            '                                    <td></td>' +
                                            '                                    <td>序号</td>' +
                                            '                                    <td colspan="2">物料描述</td>' +
                                            '                                    <td>数量</td>' +
                                            '                                    <td>单位</td>' +
                                            '                                    <td colspan="3">型号/标准号</td>' +
                                            '                                    <td>品牌</td>' +
                                            '                                    <td>备注</td>' +
                                            '                                    <td>原因</td>' +
                                            '                                    <td>附件</td>' +
                                            '                                </tr>');
                                        let beforeData = result.data.before;
                                        for (let i = 0; i < beforeData.length; i++) {
                                            $("#tableBody").append('<tr id="before_' + i + '" style="vertical-align: middle;font-size: 16px">' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;width: 60px;"><p style="word-wrap: break-word;padding: 5px">原提报型号</p></td>' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + eval(Number(i) + 1) + '</p></td>' +
                                                '                                    <td colspan="2" style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].malDesc === null ? '' : beforeData[i].malDesc) + '</p></td>' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].num === null ? '' : beforeData[i].num) + '</p></td>' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].unit === null ? '' : beforeData[i].unit) + '</p></td>' +
                                                '                                    <td colspan="3" style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].standard === null ? '' : beforeData[i].standard) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].brand === null ? '' : beforeData[i].brand) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].remark == null ? '' : beforeData[i].remark) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (beforeData[i].changeReason == null ? '' : beforeData[i].changeReason) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;text-align: center"><a href="' + (beforeData[i].url == null ? '#' : '/buy/getReportPlanList/download/' + result.data.resultId+'/'+1) + '">'+(beforeData[i].url == null ? '' : '下载')+'</a></td>' +
                                                '                                </tr>');
                                        }
                                        $("#tableBody").append('<tr><td colspan="13" style="height: 5px"></td></tr>')
                                        let afterData = result.data.after;
                                        for (let i = 0; i < afterData.length; i++) {
                                            $("#tableBody").append('<tr id="after_' + i + '" style="vertical-align: middle;font-size: 16px">' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;width: 60px;"><p style="word-wrap: break-word;padding: 5px">更改后型号</p></td>' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + eval(Number(i) + 1) + '</p></td>' +
                                                '                                    <td colspan="2" style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].malDesc === null ? '' : afterData[i].malDesc) + '</p></td>' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].num === null ? '' : afterData[i].num) + '</p></td>' +
                                                '                                    <td style="font-size: 16px;text-align: center;vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].unit === null ? '' : afterData[i].unit) + '</p></td>' +
                                                '                                    <td colspan="3" style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].standard === null ? '' : afterData[i].standard) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].brand === null ? '' : afterData[i].brand) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].remark == null ? '' : afterData[i].remark) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;"><p style="word-wrap: break-word;padding: 5px">' + (afterData[i].changeReason == null ? '' : afterData[i].changeReason) + '</p></td>' +
                                                '                                    <td style="vertical-align: middle;text-align: center"><a href="' + (afterData[i].url == null ? '#' : '/buy/getReportPlanList/download/' + result.data.resultId+'/'+0) + '">'+(afterData[i].url == null ? '' : '下载')+'</a></td>' +
                                                '                                </tr>');
                                        }
                                        reportPlanArray = new Array();
                                        let id = 0;
                                        for (let i = 0; i < result.data.list.length; i++) {
                                            if (result.data.list[i].dataType === '变更前') {
                                                id = result.data.list[i].id;
                                                break;
                                            }
                                        }
                                        for (let i = 0; i < result.data.list.length; i++) {
                                            let newObj = $.extend({}, result.data.list[i]);
                                            if (result.data.list[i].dataType === '变更后') {
                                                newObj.id = id;
                                            }
                                            if (result.data.list[i].dataType != '变更前') {
                                                reportPlanArray.push(newObj);
                                            }
                                        }
                                        js.modal.open('modal-change');
                                        loadChangePlanList(result.data.list);
                                    }
                                })
                            } else {
                                js.post({
                                    url: ctx + "buy/getReportPlanList/receive/plans",
                                    data: {
                                        ids: id
                                    },
                                    success: function (result) {
                                        if (result.type === web_status.SUCCESS) {
                                            js.modal.success(result.msg);
                                            js.table.refresh(table);
                                        } else {
                                            js.modal.warning(result.msg);
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else {
                    js.modal.success("此计划已撤销");
                    js.table.refresh(table);
                }
            }
        })
    }

    /**
     * 打开编辑窗口
     * */
    function edit(id) {
        js.post({
            url: ctx + "plan/change/getChangePlan",
            data: {
                rppId: id
            },
            success: function (result) {
                $("#after").fillSpan(result.data);
                let row = $("#bootstrap-table-change-view").bootstrapTable('getRowByUniqueId', id);
                $("#change").fillData(row);
                $("#change [name='id']").val(id);
                let useStore = result.data.afterUseStore === 0 ? '否' : result.data.afterUseStore === 1 ? '是' : '';
                $("#afterUseStore").text(useStore);
                if(result.data.rppTypes==='外协件'){
                    $("#afterStandardDiv").hide();
                    $("#afterUseStoreDiv").hide();
                    $("#standardDiv").hide();
                    $("#useStoreDiv").hide();
                    $("#afterDiagramDiv").show();
                    $("#afterAssistTypeDiv").show();
                    $("#diagramDiv").show();
                    $("#assistTypeDiv").show();
                }else if(result.data.rppTypes==='外购件'){
                    $("#afterStandardDiv").show();
                    $("#afterUseStoreDiv").show();
                    $("#standardDiv").show();
                    $("#useStoreDiv").show();
                    $("#afterDiagramDiv").hide();
                    $("#afterAssistTypeDiv").hide();
                    $("#diagramDiv").hide();
                    $("#assistTypeDiv").hide();
                }else if(result.data.rppTypes==='原材料'){
                    $("#afterStandardDiv").show();
                    $("#afterUseStoreDiv").show();
                    $("#standardDiv").show();
                    $("#useStoreDiv").show();
                    $("#afterDiagramDiv").hide();
                    $("#afterAssistTypeDiv").hide();
                    $("#diagramDiv").hide();
                    $("#assistTypeDiv").hide();
                }
                js.modal.open('modal-edit');
            }
        })

    }

    /**
     * 加载表
     * @param data
     */
    function loadChangePlanList(data) {
        $("#bootstrap-table-change-view").bootstrapTable('destroy');
        changeTable = js.table.init({
            id: "bootstrap-table-change-view",
            showColumns: false,
            showExport: false,
            data: data,
            pageSize: 100000,
            smartDisplay: false,
            pagination: false,
            uniqueId: 'id',
            paginationVAlign: 'both',
            columns: [
                {
                    title: '序号', field: 'id', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: "说明", field: 'dataType', width: '100'},
                {title: "物料描述", field: 'malDesc'},
                {title: "数量", field: 'num'},
                {title: "单位", field: 'unit'},
                {
                    title: "型号/标准号", field: 'standard'
                    , formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "图号", field: 'diagram'
                    , formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "品牌", field: 'brand', formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "外协类型", field: 'assistType'
                    , formatter: function (value, row, index) {
                        if (value != null) {
                            return value;
                        } else {
                            return "--";
                        }
                    }
                },
                {title: "备注", field: 'remark'},
                {
                    title: "是否使用库存", field: 'useStore',
                    formatter: function (value, row, index) {
                        if (value === 0) {
                            return "否";
                        } else if (value === 1) {
                            return "是";
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "提报状态", field: 'useStock',
                    formatter: function (value, row, index) {
                        if(value===4){
                            var ii = 10;
                        }
                        return '<span class="badge ' + reportColors[value] + '">' + reportState[value] + '</span> ';
                    }
                },
                {
                    title: "附件", field: 'url', formatter: function (value, row, index) {
                        if (value != null) {
                            let elements = [];
                            let url = row.url;
                            if (hasP('buy:planchangereceive:download') && row.url != null) {
                                elements.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="look2(' + row.id + ',\'' + row.dataType + '\')"><i class="fa  fa-search"></i> 查看</a> ');
                                elements.push('<a class="btn btn-primary btn-xs" href="/buy/getReportPlanList/download/' + row.rppResultId+'/1"><i class="glyphicon glyphicon-download-alt"></i> 下载</a> ');
                            }
                            return elements.join('');
                        } else {
                            return "--";
                        }
                    }
                },
                {
                    title: "操作",
                    width: '110px',
                    formatter: function (value, row, index) {
                        let actions = [];
                        if (row.dataType === '拆分记录' || row.dataType === '变更前') {
                            actions.push('<button class="btn btn-warning btn-xs" onclick="edit(' + row.id + ')"> 编辑</button> ');
                        }
                        return actions.join('');
                    }
                }
            ]
        });
    }

    /**
     * 文本框限制输入数字
     * @param obj
     */
    function numInput(obj) {
        if (obj.value < 0) {
            obj.value = 0;
        } else if (obj.value.length > 0 && ((obj.value[0] === '0' && obj.value[1] === '.') || !(obj.value[0] === '0' && obj.value[1] != '.'))) {
            obj.value = obj.value.replace(/[^-\d.]/g, "");  //清除“数字”和“.”以外的字符
            obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
            obj.value = obj.value.replace(/\-{2,}-/g, "-"); //只保留第一个. 清除多余的
            obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
            obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d\d).*$/, '$1$2.$3');//只能输入两个小数
            if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                obj.value = obj.value;
            }
        } else {
            obj.value = '0';
        }
    }

    /**
     * 检测物控员是不是计划接收者
     * @param employName
     */
    function checkEmployIsRecipient(employName) {
        let resultFlag = false;
        js.post({
            url: ctx+'plan/change/checkEmployIsRecipient',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {
                empName:employName
            },
            async: false,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    resultFlag = result.data;
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
        return resultFlag;
    }
</script>
</body>
</html>